package com.apestech.api.query.mapping;

import com.apestech.framework.json.JSONArray;
import com.apestech.framework.json.JSONObject;
import com.apestech.framework.sql.modul.FieldCondition;

import java.util.ArrayList;
import java.util.Map;

/**
 * YYERP老框架查询条件处理
 *
 * @author hup
 * @create 2018-08-16-10:46
 */
public class EQMapping {

    private JSONObject param;
    private JSONObject result;
    private JSONArray conlist;

    public EQMapping(JSONObject param){
        this.param=param;
    }

    public JSONObject getResult() {
        return result;
    }

    public void expain(){
        result=new JSONObject();
        conlist=new JSONArray();
        ArrayList<String> klist=param.getKeylist();
        int len=klist.size();
        for(int i=0; i<len; i++){
            String field=klist.get(i);
            Object value=param.get(field);
            if("F_CZQX".equals(field) || ("F_CXQX").equals(field)
                    || ("page").equals(field) || ("size").equals(field) || ("rows").equals(field)
                    || ("bcx").equals(field) || ("totle").equals(field)
                    || ("F_BDJS").equals(field) || ("type").equals(field)
                    || ("qrylx").equals(field) || ("F_SUMFIELD").equals(field)
                    || ("queryid").equals(field) ){
                switch (field){
                    case "qrylx":{
                        field="queryid";
                        if(result.get("queryid")!=null) value=null;
                        break;
                    }
                    case "rows":{
                        field="size";
                        break;
                    }
                    case "F_BDJS":{
                        field="postid";
                        break;
                    }
                    case "F_SUMFIELD":{
                        field="sumfield";
                        break;
                    }
                }
                if(value!=null) result.put(field, value);
            } else {
                addField(field, value);
            }
        }
        if(conlist.size()>0){
            result.put("condition",conlist);
        }
        result.put("type", "page");  //eq-page
    }

    private void addField(String field, Object value){
        if(value==null) return;
        String operator="EQ";
        Object val=null;
        if(value instanceof Map){
            JSONObject m=new JSONObject((Map) value);
            String gs=m.getString("GS");
            if(gs!=null){
                operator=getExpress(gs);
                val=m.get(field);
                conlist.add(new FieldCondition(field, operator, val, null));
            } else {
                Object sval=m.get(field+"_ST");
                if(sval!=null){
                    operator=getExpress(m.getString("GSST"));
                    val=m.get(field+"_ST");
                    conlist.add(new FieldCondition(field, operator, val, "date"));
                }
                sval=m.get(field+"_EN");
                if(sval!=null){
                    operator=getExpress(m.getString("GSEN"));
                    val=m.get(field+"_EN");
                    conlist.add(new FieldCondition(field, operator, val, "date"));
                }
            }
        } else {
            val=value;
            if(value instanceof String){
                if(((String) value).indexOf(";")>=0){
                    String[] slist=((String) value).split(";");
                    ArrayList<String> dlist=new ArrayList<String>();
                    int len=slist.length;
                    for(int i=0; i<len; i++){
                        dlist.add(slist[i]);
                    }
                    val=dlist;
                    operator="IN";
                }
            } else if(value instanceof ArrayList){
                operator="IN";
            }
            FieldCondition con=new FieldCondition(field, operator, val, null);
            Object sval=param.get("Q_"+field);
            if(sval!=null && sval instanceof Map){
                sval=new JSONObject((Map)sval);
                con.setId(((JSONObject) sval).getString("id"));
                con.setTree(((JSONObject) sval).getString("tree"));
                con.setParentid("P_ID");
                con.setType("tree");
            }
            conlist.add(con);
        }
    }

    private String getExpress(String gs){
        switch (gs){
            case "EQ": return "EQ";
            case "NEQ":return "NE";
            case "ADD":return "GT";
            case "ADDE":return "GTE";
            case "DEC":return "LT";
            case "DECE":return "LTE";
            case "NUL":return "IS_NULL";
            case "NNUL":return "IS_NOT_NULL";
            case "STLIKE":return "RLIKE";
            case "ENLIKE":return "LLIKE";
            case "EIN":return "IN";
            case "ENIN":return "NIN";
            default: return gs;
        }
    }

}
